package com.qk.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.dto.BusinessDto;
import com.qk.entity.Business;
import com.qk.service.BusinessService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.time.LocalDateTime;

@Slf4j
@RestController
@RequestMapping("/businesses")
public class BusinessController {

    @Autowired
    private BusinessService businessService;

    /**
     * 添加业务
     */
    @PostMapping
    public Result add(@RequestBody Business business){
        log.info("添加业务：{}", business);
        business.setCreateTime(LocalDateTime.now());
        business.setUpdateTime(LocalDateTime.now());
        businessService.save(business);
        return Result.success();
    }

    /**
     * 列表查询
     */
    @GetMapping
    public Result listBusiness(BusinessDto businessDto){
        log.info("查询参数：{}", businessDto);
        PageResult< Business> pageResult = businessService.listBusiness(businessDto);
        return Result.success(pageResult);
    }

    /**
     * 分配商机
     */
    @PutMapping("/assign/{businessId}/{userId}")
    public Result assign(@PathVariable Integer businessId, @PathVariable Integer userId){

        log.info("分配商机：{}, {}", businessId , userId);
        Business business = new Business();
        business.setId(businessId);
        business.setUserId(userId);
        business.setStatus(2);
        business.setUpdateTime(LocalDateTime.now());
        businessService.updateById(business);
        return Result.success();

    }

    /**
     * 根据id查询回显
     */
    @GetMapping("/{id}")
    public Result getBusinessByid(@PathVariable Integer id){
        log.info("查询id:{}", id);
        Business business = businessService.getBusinessById(id);
        return Result.success(business);
    }

    /**
     * 商机跟进
     */
    @PutMapping
    public Result trankBusiness(@RequestBody Business business){
        log.info("商机跟进：{}", business);
        businessService.tankBusiness(business);
        return Result.success();
    }

    /**
     * 公海池
     */
    @GetMapping("/pool")
    public Result businessPool(BusinessDto businessDto){
        log.info("公海池查询参数：{}", businessDto);
        PageResult< Business> pageResult = businessService.listBusinessPool(businessDto);
        return Result.success(pageResult);
    }

    /**
     * 踢回公海
     */
    @PutMapping("/back/{id}")
    public Result kickBack(@PathVariable Integer id){
        log.info("踢回公海的商机ID：{}", id);
        businessService.kickBack(id);
        return Result.success();
    }

    /**
     * 转客户
     */
    @PostMapping("/toCustomer/{id}")
    public Result turnCustomer(@PathVariable Integer id){
        log.info("转为客户的商机ID：{}", id);
        businessService.turnCustomer(id);
        return Result.success();
    }


}
